﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SockLib.Device.ModbusDevices
{
    /// <summary>
    /// <para>大棚控制器,Did=2,DOper如下：</para>
    /// <para>1:读数据</para>
    /// </summary>
    internal class GreenHouse : MBType
    {
        /// <summary>
        /// SOCKET连接的ip地址
        /// </summary>
        public string IPAdress { get; set; }

        /// <summary>
        /// 大棚控制器
        /// </summary>
        public GreenHouse()
        { }

        /// <summary>
        /// modbus数据分析
        /// </summary>
        /// <param name="buffer">收到的数据</param>
        /// <param name="res">解析后的字符串</param>
        /// <returns></returns>
        public bool DataAnaly(byte[] buffer,ref  ModbusData  res)
        {
            res = new Device.ModbusData();
           if(! SockLib.Device.CRC  . CheckCrc(buffer,false ))  //CRC校验是否正确
                return false ;
            res.device_id = buffer[0];
            res.device_cmd = buffer[1];
            res.recdata = buffer;
            res.IsLowBitFirst = true;
            res.Dtype = GetDeviceType();
            res.Did = GetDeviceId();
            res.data = new Dictionary<string, object>() ;
            res.DOper = 1;
            res.data.Add("key1", "value1");
            res.data.Add("key2", "value2");
            return true;
        }

        /// <summary>
        /// 获取modbus设备信息
        /// </summary>
        /// <returns></returns>
        public string GetDeviceType()
        {
            return "ModBus_大棚";
        }

        /// <summary>
        /// 获得modbus设备类型ID
        /// </summary>
        /// <returns></returns>
        public int GetDeviceId()
        {
            return 1;
        }
    }
}
